//
//  XS_Image.swift
//  WorkDemo
//
//  Created by 韩云智 on 2019/9/30.
//  Copyright © 2019 韩云智. All rights reserved.
//

import Foundation
import UIKit
import RxSwift
import RxCocoa
import SnapKit

public extension UIView {
    /// view 转换成 Image
    func xsToImage(_ size: CGSize? = nil) -> UIImage? {
        //        UIGraphicsBeginImageContext(view.bounds.size)
        let size = size ?? bounds.size
        UIGraphicsBeginImageContextWithOptions(size, false, 2.0)
        if let context = UIGraphicsGetCurrentContext() {
            layer.render(in: context)
        }
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }
    /// 部分圆角
    ///
    /// - Parameters:
    ///   - corners: 需要实现为圆角的角，可传入多个
    ///   - radii: 圆角半径
    func corner(byRoundingCorners corners: UIRectCorner, radii: CGFloat, isShadow: Bool = false, shadowOffset: CGSize = CGSize(width: 0, height: 0), shadowOpacity: Float = 0.54) {
        let maskPath = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radii, height: radii))
        let maskLayer = CAShapeLayer()
        maskLayer.frame = bounds
        maskLayer.path = maskPath.cgPath
        layer.mask = maskLayer
        if isShadow {
            let shadowLayer = CALayer()
            shadowLayer.frame = frame
            shadowLayer.shadowColor = #colorLiteral(red: 0.5, green: 0.5, blue: 0.5, alpha: 1)
            shadowLayer.shadowOpacity = shadowOpacity
            shadowLayer.shadowOffset = shadowOffset
            shadowLayer.shadowRadius = 3
            shadowLayer.shadowPath = maskPath.cgPath
            superview?.layer.insertSublayer(shadowLayer, below: layer)
        }
    }
    /// 删除所有子视图
    func removeSubviews(_ buts: [Int] = []) {
        for subView in subviews {
            if buts.contains(subView.tag) { continue }
            subView.snp.removeConstraints()
            subView.removeFromSuperview()
        }
    }
    /// 改变锚点
    func changeAnchorPoint(_ point: CGPoint, size: CGSize? = nil) {
        let size = size ?? bounds.size
        layer.anchorPoint = CGPoint(x: point.x/size.width, y: point.y/size.height)
        layer.transform = CATransform3DMakeTranslation(point.x-size.width/2, point.y-size.height/2, 0)
    }
}
public extension UIImage {
    /// 设置拉伸位置
    func xs_resizable(_ capInsets: UIEdgeInsets, size: CGSize? = nil, resizingMode: UIImage.ResizingMode = .stretch) -> UIImage {
        guard let size = size, size != self.size else {
            return resizableImage(withCapInsets: capInsets, resizingMode: resizingMode)
        }
        UIGraphicsBeginImageContext(size)
        draw(in: CGRect(origin: .zero, size: size))
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        guard let img = newImage else {
            return resizableImage(withCapInsets: capInsets, resizingMode: resizingMode)
        }
        return img.resizableImage(withCapInsets: capInsets, resizingMode: resizingMode)
    }
}
